/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmDatPhong.java
 *
 * Created on Dec 6, 2011, 9:02:31 AM
 */
package uit.qlks.from;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import uit.ejb.dto.KhachHangDTO;
import uit.ejb.dto.PhieuThueDTO;
import uit.ejb.dto.PhieuThueDVDTO;
import uit.qlks.controller.khachhang.KhachHangController;
import uit.qlks.controller.loaidichvu.LoaiDichVuController;
import uit.qlks.controller.loaiphong.LoaiPhongCotroller;
import uit.qlks.controller.phieuthue.PhieuThueController;
import uit.qlks.controller.phieuthuedv.PhieuThueDVController;
import uit.qlks.controller.phong.PhongController;

/**
 *
 * @author QUANGTHIEU
 */
public class frmDatPhong extends javax.swing.JFrame {
private List MaLoaiPhong = new ArrayList();
private List MaPhong = new ArrayList();
private List GiaPhong = new ArrayList();
private boolean active ;
public static frmDatPhong _frm;
//dung cho ben phieu thue
String MaKH;
String MaPT;
String MP;
int preframe = 0;
//dung cho dat dich vu
List MaDV = new ArrayList();
List SoLuong = new ArrayList();
JTable BangDV;
Hashtable<String, String> GiaDV = new Hashtable<String, String>();
private String MaLP;//luu lai MaLoaiPhong khi dat
    /** Creates new form frmDatPhong */
    public frmDatPhong() {
        initComponents();
        active = false;
        lbcanhbao.setVisible(false);
        btnSua1.setEnabled(false);
        btnXoa1.setEnabled(false);
        try
        {
            ResultSet rs = LoaiPhongCotroller.loaiPhong_GetByAll();
            if(rs != null)
            {
                while(rs.next())
                {
                    cmbloaiphong.addItem(rs.getString("TenLoaiPhong"));
                    MaLoaiPhong.add(rs.getString("MaLoaiPhong"));
                    GiaPhong.add(Format_Price(rs.getString("Gia")));
                }

            }
            
            active = true;
            cmbloaiphong.setSelectedIndex(0);
            txtgiaphong.setText(GiaPhong.get(0).toString());

            rs.close();
            rs = null;
        }
        catch(Exception e)
        {
             JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cmbloaiphong = new javax.swing.JComboBox();
        cmbmaphong = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        lbGia = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        txtgiaphong = new javax.swing.JTextField();
        cmdichvu = new javax.swing.JComboBox();
        dtngaythue = new com.toedter.calendar.JDateChooser();
        dtngaydi = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        txtgiadichvu = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        txtsoluong = new javax.swing.JTextField();
        jSeparator2 = new javax.swing.JSeparator();
        lbcanhbao = new javax.swing.JLabel();
        txtmapt = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        btdatdichvu = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        cmbgioitinh = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txthoten = new javax.swing.JTextField();
        txtdiachi = new javax.swing.JTextField();
        txtcmnd = new javax.swing.JTextField();
        txtsodienthoai = new javax.swing.JTextField();
        dtngaysinh = new com.toedter.calendar.JDateChooser();
        txtmakh = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        lcquoctich = new com.toedter.components.JLocaleChooser();
        jPanel3 = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        btnXoa1 = new javax.swing.JButton();
        btnThem1 = new javax.swing.JButton();
        btnSua1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Phiếu Đặt Phòng");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Times New Roman", 0, 24));
        jLabel1.setText("Phiếu Đặt Phòng");

        jPanel1.setName("pane11"); // NOI18N

        jLabel3.setText("Mã Phòng:");

        jLabel4.setText("Loại Dịch Vụ:");

        cmbloaiphong.setName("cmbloaiphong"); // NOI18N
        cmbloaiphong.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmbloaiphongItemStateChanged(evt);
            }
        });

        cmbmaphong.setName("cmbmaphong"); // NOI18N

        jLabel5.setText("Giá:");

        lbGia.setText("Ngày Đi:");
        lbGia.setName("lbGia"); // NOI18N

        jLabel17.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel17.setText("Thông Tin Đặt Phòng");

        jLabel14.setText("Loại Phòng:");

        jLabel19.setText("Ngày Thuê:");

        txtgiaphong.setEditable(false);

        cmdichvu.setName("cmbMaDV"); // NOI18N
        cmdichvu.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cmdichvuItemStateChanged(evt);
            }
        });

        dtngaythue.setDateFormatString("dd/M/yyyy");
        dtngaythue.setMinSelectableDate(new java.util.Date(-2209010318000L));
        dtngaythue.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtngaythuePropertyChange(evt);
            }
        });

        dtngaydi.setDateFormatString("dd/M/yyyy");
        dtngaydi.setMinSelectableDate(new java.util.Date(-2209010311000L));
        dtngaydi.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                dtngaythuePropertyChange(evt);
            }
        });

        jLabel6.setText("Giá:");

        txtgiadichvu.setEditable(false);

        jLabel7.setText("Số Lượng:");

        txtsoluong.setEditable(false);
        txtsoluong.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcmndKeyTyped(evt);
            }
        });

        jSeparator2.setOrientation(javax.swing.SwingConstants.VERTICAL);

        lbcanhbao.setForeground(new java.awt.Color(255, 4, 4));
        lbcanhbao.setText("Hiện Tại Đã Hết Phòng!");

        txtmapt.setBackground(new java.awt.Color(204, 204, 204));
        txtmapt.setEditable(false);

        jLabel18.setText("Mã PT:");

        btdatdichvu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/icondv.png"))); // NOI18N
        btdatdichvu.setText("Đặt dịch vụ");
        btdatdichvu.setBorder(null);
        btdatdichvu.setContentAreaFilled(false);
        btdatdichvu.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/icondv2.png"))); // NOI18N
        btdatdichvu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btdatdichvuActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(32, 32, 32)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel14)
                                    .addComponent(jLabel19)
                                    .addComponent(lbGia))
                                .addGap(5, 5, 5))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel3)
                                .addComponent(jLabel4))
                            .addComponent(jLabel18)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(btdatdichvu)))
                .addGap(36, 36, 36)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtmapt, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmdichvu, 0, 182, Short.MAX_VALUE)
                                    .addComponent(dtngaythue, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cmbmaphong, 0, 182, Short.MAX_VALUE)
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                        .addComponent(cmbloaiphong, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(dtngaydi, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jLabel17, javax.swing.GroupLayout.Alignment.TRAILING))
                                .addGap(18, 18, 18))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(18, 18, 18)
                                .addComponent(txtsoluong, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel5)
                                    .addComponent(jLabel6))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(txtgiadichvu)
                                    .addComponent(txtgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(lbcanhbao))))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel17)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 29, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dtngaythue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel19))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(dtngaydi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbGia))
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbloaiphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14)
                    .addComponent(jLabel5)
                    .addComponent(txtgiaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbmaphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbcanhbao)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmdichvu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btdatdichvu)
                            .addComponent(jLabel7)
                            .addComponent(txtsoluong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel6)
                        .addComponent(txtgiadichvu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 18, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(txtmapt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jSeparator2, javax.swing.GroupLayout.DEFAULT_SIZE, 329, Short.MAX_VALUE)
                .addContainerGap())
        );

        jPanel2.setName("pane11"); // NOI18N

        jLabel8.setText("Họ Tên:");

        jLabel9.setText("Giới Tính:");

        cmbgioitinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Không xác định", "Nam", "Nữ" }));
        cmbgioitinh.setName("cmbMaDV"); // NOI18N

        jLabel10.setText("Ngày Sinh:");

        jLabel11.setText("Địa Chỉ:");

        jLabel12.setText("CMND:");

        jLabel13.setText("Số Điện Thoại:");

        jLabel15.setText("Quốc Tịch:");

        jLabel16.setFont(new java.awt.Font("Times New Roman", 0, 18));
        jLabel16.setText("Thông Tin Khách Hàng");

        txtcmnd.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcmndKeyTyped(evt);
            }
        });

        txtsodienthoai.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtcmndKeyTyped(evt);
            }
        });

        dtngaysinh.setDateFormatString("dd/M/yyyy");
        dtngaysinh.setMaxSelectableDate(new java.util.Date(253370743273000L));
        dtngaysinh.setMinSelectableDate(new java.util.Date(-2209010327000L));

        txtmakh.setBackground(new java.awt.Color(204, 204, 204));
        txtmakh.setEditable(false);

        jLabel20.setText("Mã KH:");

        lcquoctich.setSelectedIndex(82);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel10)
                            .addComponent(jLabel8)
                            .addComponent(jLabel9)
                            .addComponent(jLabel15)
                            .addComponent(jLabel12)
                            .addComponent(jLabel20))
                        .addGap(48, 48, 48)
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lcquoctich, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(txtsodienthoai, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtcmnd, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txtdiachi, javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(txthoten, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)
                                .addComponent(cmbgioitinh, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(dtngaysinh, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(txtmakh, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jLabel13))
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(130, Short.MAX_VALUE)
                .addComponent(jLabel16)
                .addGap(68, 68, 68))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel16)
                        .addGap(26, 26, 26)
                        .addComponent(txthoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel8))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmbgioitinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel10)
                    .addComponent(dtngaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(txtdiachi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtcmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel12))
                .addGap(13, 13, 13)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtsodienthoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(lcquoctich, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtmakh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel20))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnXoa1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete.png"))); // NOI18N
        btnXoa1.setText("Hủy");
        btnXoa1.setBorder(null);
        btnXoa1.setContentAreaFilled(false);
        btnXoa1.setName("btnXoa"); // NOI18N
        btnXoa1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete2.png"))); // NOI18N
        btnXoa1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoa1ActionPerformed(evt);
            }
        });

        btnThem1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add.png"))); // NOI18N
        btnThem1.setText("Đặt phòng");
        btnThem1.setBorder(null);
        btnThem1.setContentAreaFilled(false);
        btnThem1.setName("btnThem"); // NOI18N
        btnThem1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add2.png"))); // NOI18N
        btnThem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThem1ActionPerformed(evt);
            }
        });

        btnSua1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit.png"))); // NOI18N
        btnSua1.setText("Sửa");
        btnSua1.setBorder(null);
        btnSua1.setContentAreaFilled(false);
        btnSua1.setName("btnSua"); // NOI18N
        btnSua1.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit2.png"))); // NOI18N
        btnSua1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSua1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(126, 126, 126)
                        .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 544, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addComponent(btnXoa1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(73, 73, 73)
                        .addComponent(btnThem1)
                        .addGap(69, 69, 69)
                        .addComponent(btnSua1, javax.swing.GroupLayout.PREFERRED_SIZE, 71, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(76, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(btnThem1)
                        .addComponent(btnXoa1))
                    .addComponent(btnSua1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(374, 374, 374)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(43, 43, 43)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(87, 87, 87)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(53, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-976)/2, (screenSize.height-558)/2, 976, 558);
    }// </editor-fold>//GEN-END:initComponents

    ///<editor-fold defaultstate="collapsed" desc=" check Input ">
    private boolean  checkinput()
    {

        if("".equals(txthoten.getText()))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập tên Khách Hàng","Warning", JOptionPane.WARNING_MESSAGE);
            txthoten.requestFocusInWindow();
            return false;
        }
        if("null".equals(dtngaysinh.getDate()+""))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập ngày sinh","Warning", JOptionPane.WARNING_MESSAGE);
            dtngaysinh.requestFocusInWindow();
            return false;
        }
        
        if("".equals(txtdiachi.getText()))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập địa chỉ","Warning", JOptionPane.WARNING_MESSAGE);
            txtdiachi.requestFocusInWindow();
            return false;
        }
        if("".equals(txtcmnd.getText()))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa nhập CMND","Warning", JOptionPane.WARNING_MESSAGE);
            txtcmnd.requestFocusInWindow();
            return false;
        }

        if("null".equals(dtngaythue.getDate()+""))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa chọn ngày thuê","Warning", JOptionPane.WARNING_MESSAGE);
            dtngaythue.requestFocusInWindow();
            return false;
        }
//        if(System.currentTimeMillis() > dtngaythue.getDate().getTime() + 86400000)
//        {
//            JOptionPane.showMessageDialog(rootPane, "Ngày thuê không thể nhỏ hơn ngày hiện tại.","Warning", JOptionPane.WARNING_MESSAGE);
//            dtngaythue.requestFocusInWindow();
//            return false;
//        }
        if("null".equals(dtngaydi.getDate()+""))
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa chọn ngày trả phòng","Warning", JOptionPane.WARNING_MESSAGE);
            dtngaydi.requestFocusInWindow();
            return false;
        }
         
        if(dtngaydi.getDate().getTime() < dtngaythue.getDate().getTime())
        {
            JOptionPane.showMessageDialog(rootPane, "Ngày trả phòng phải sau ngày thuê phòng","Warning", JOptionPane.WARNING_MESSAGE);
            dtngaydi.requestFocusInWindow();
            return false;
        }
        if(cmbmaphong.getSelectedIndex() == -1)
        {
            JOptionPane.showMessageDialog(rootPane, "Chưa chọn phòng","Warning", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if(cmdichvu.getSelectedIndex() == -1 && btnThem1.isEnabled())
        {
          if(  JOptionPane.showConfirmDialog(null,  "Bạn có muốn đặt dịch vụ không?"
                    , "Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
              return false;
          
        }
        
        return true;
    }//</editor-fold>
    
    ///<editor-fold defaultstate="collapsed" desc=" setreset ">
    private void setreset()
    {
        txthoten.setText("");
        txtdiachi.setText("");
        txtcmnd.setText("");
        txtsodienthoai.setText("");
        txtsoluong.setText("1");
        txtmakh.setText("");
        txtmapt.setText("");
        
        dtngaysinh.setDate(null);
        dtngaydi.setDate(null);
        dtngaythue.setDate(null);
        
        cmbloaiphong.setSelectedIndex(0);
        txtgiaphong.setText(GiaPhong.get(0).toString());
//        cmdichvu.setSelectedIndex(-1);
        cmdichvu.removeAllItems();
        txtgiadichvu.setText("");
        txtsoluong.setText("");
        frmddv = null;
        txtgiadichvu.setText("");
        cmbmaphong.setSelectedIndex(-1);
        
        btnThem1.setEnabled(true);
        btnSua1.setEnabled(false);
        btnXoa1.setEnabled(false);
    }
    //</editor-fold>
    
    ///<editor-fold defaultstate="collapsed" desc=" Format_Price ">
    private String Format_Price(String Price)
        {
            if ("0".equals(Price))
            {
                return "";
            }
            else
                Price = Price.replace(".0", "");
            Price = Price.replace(",", "");
            String tmp = "";
            while (Price.length() > 3)
            {
                tmp = "." + Price.substring(Price.length() - 3) + tmp;
                Price = Price.substring(0, Price.length() - 3);
            }
            tmp = Price + tmp + " VNĐ";
            return tmp;
        }
    //</editor-fold>
    
    ///<editor-fold defaultstate="collapsed" desc=" set-phieudatphong ">
    public void set_phieudatphong()
    {
        try
        {
            active = false;
            cmbloaiphong.setSelectedIndex(-1);
            //lay thong tin khach hang
            ResultSet rs = KhachHangController.khachHang_GetById(MaKH);
            if(rs!=null)
            {
                while(rs.next())
                {
                    txthoten.setText(rs.getString("TenKH"));
                    txtdiachi.setText(rs.getString("DiaChi"));
                    txtcmnd.setText(rs.getString("CMND"));
                    txtsodienthoai.setText(rs.getString("SDT"));
                    txtmakh.setText("KH"+MaKH);
                    cmbgioitinh.setSelectedItem(rs.getString("GioiTinh"));
                    dtngaysinh.setDate(rs.getDate("NgaySinh"));
                }
            }
            //lay thong tin dat phong
            rs = PhieuThueController.phieuThue_GetById(MaPT);
            while(rs.next())
            {
                dtngaythue.setDate(rs.getDate("NgayThue"));
                dtngaydi.setDate(rs.getDate("NgayDi"));
            }
            //lay thong tin phong
            String TenPhong = "";
            rs = PhongController.phong_GetById(MP);
            while(rs.next())
            {
               // JOptionPane.showConfirmDialog(rootPane,rs.getString("MaLoaiPhong"));
                MaLP = rs.getString("MaLoaiPhong");
                TenPhong = rs.getString("TenPhong");
            }
            //lay loai phong va gia
            rs = LoaiPhongCotroller.loaiPhong_GetById(MaLP);
            active = true;
            btnSua1.setEnabled(true);
           // if(rs == null) JOptionPane.showConfirmDialog(rootPane, "NULL");
            while(rs.next())
            {
                //JOptionPane.showConfirmDialog(rootPane, rs.getString("TenLoaiPhong"));
                cmbloaiphong.setSelectedItem(rs.getString("TenLoaiPhong"));//tu dong cap nhat gia cho txtgia
            }
            //set maphong
            cmbmaphong.setSelectedItem(TenPhong);
            txtmapt.setText("PT"+MaPT);
            //thong tin dich vu
            ResultSet resdv;
            String _MaDV;
            resdv = LoaiDichVuController.loaiDichVu_GetByAll();
            Hashtable<String, String> temp = new Hashtable<String, String>();
            while(resdv.next())
            {
                _MaDV = resdv.getString("MaDV");
                GiaDV.put(_MaDV, Format_Price(resdv.getString("Gia")));
                temp.put(_MaDV, resdv.getString("TenDV"));
            }
            rs = PhieuThueDVController.phieuThueDV_GetById(MaPT);
            if(rs != null)
            {
                while(rs.next())
                {
                    _MaDV = rs.getString("MaDV");
                    SoLuong.add(rs.getString("SoLuong"));
                    cmdichvu.addItem(temp.get(_MaDV));
                    MaDV.add(_MaDV);
                }               
            }
            if(cmdichvu.getSelectedIndex() != -1)
            {
                txtgiadichvu.setText(GiaDV.get(MaDV.get(cmdichvu.getSelectedIndex()).toString()));
                txtsoluong.setText(SoLuong.get(cmdichvu.getSelectedIndex()).toString());
                btdatdichvu.setText("Sửa dịch vụ");
            }
            temp = null;
            //set cho form dat dich vu
            frmddv = new frmDatDichVu();//tao form dat dich vu
            frmddv.MaDVDat = MaDV;
            frmddv.SoLuongDat = SoLuong;
            frmddv.setopen();
            //khoa button
//            btnSua1.setEnabled(true);
            btnThem1.setEnabled(false);
            btnXoa1.setEnabled(false);
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    //</editor-fold>
  
    
private void btnThem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThem1ActionPerformed
        try {

            //kiem tra input
            if(!checkinput()) return;

            //Luu thong tin khac hang
            KhachHangDTO kh = new KhachHangDTO();
            kh.setTenKH(txthoten.getText());
            kh.setGioiTinh(cmbgioitinh.getSelectedItem().toString());
            kh.setDiaChi(txtdiachi.getText());
            kh.setNgaySinh((dtngaysinh.getDate().getMonth() + 1 )+"/"+ dtngaysinh.getDate().getDate()+ "/" + (dtngaysinh.getDate().getYear() + 1900));
            kh.setQuocTich(lcquoctich.getSelectedItem().toString());
            kh.setCMND(txtcmnd.getText());
            kh.setSDT(txtsodienthoai.getText());

            KhachHangController.khachHang_Insert(kh);
            
            MaKH = KhachHangController.khachHang_GetMaxMaKH();
            
            //luu phieu thue
            PhieuThueDTO pt = new PhieuThueDTO();
            pt.setMaKH(MaKH);
            MP = (String) MaPhong.get(cmbmaphong.getSelectedIndex());
            pt.setMaPhong((String) MaPhong.get(cmbmaphong.getSelectedIndex()));
            pt.setNgayDi((dtngaydi.getDate().getMonth() + 1 )+ "/"+ dtngaydi.getDate().getDate()+ "/" + (dtngaydi.getDate().getYear() + 1900));
            pt.setNgayThue((dtngaythue.getDate().getMonth() + 1 )+ "/"+ dtngaythue.getDate().getDate()+ "/" + (dtngaythue.getDate().getYear() + 1900));
            //JOptionPane.showMessageDialog(rootPane, pt.NgayDi);
            PhieuThueController.phieuThue_Insert(pt);
            
            MaPT = PhieuThueController.phieuThue_GetMaxMaPT();
            //cap nhat tinh trang phong
            PhongController.phong_UpdateStatus(pt.getMaPhong(), "0");
            //luu phieu thue dich vu
            PhieuThueDVDTO dv = new PhieuThueDVDTO();
            for(int i = 0; i< MaDV.size();i++)
            {
                dv.setMaPhieuThue(MaPT);
                dv.setMaPTDV(MaDV.get(i).toString());
                dv.setSoLuong(SoLuong.get(i).toString());
                dv.setGhiChu("");
                PhieuThueDVController.phieuThueDV_Insert(dv);
            }
            //Luu lai MaLoaiPhong vua dat
            MaLP = MaLoaiPhong.get(cmbloaiphong.getSelectedIndex()).toString();
            JOptionPane.showMessageDialog(rootPane, "Đặt phòng thành công!");

            txtmakh.setText("KH"+MaKH);
            txtmapt.setText("PT"+MaPT);
            
            btnThem1.setEnabled(false);
            btnSua1.setEnabled(true);
            btnXoa1.setEnabled(true);
            //JOptionPane.showConfirmDialog(rootPane, lcquoctich.getSelectedItem());
            // TODO add your handling code here:
        } catch (Exception ex) {
            Logger.getLogger(frmDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_btnThem1ActionPerformed

private void cmbloaiphongItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbloaiphongItemStateChanged
   try
   {
       if(active)
       {
        txtgiaphong.setText(GiaPhong.get(cmbloaiphong.getSelectedIndex()).toString());
        cmbmaphong.removeAllItems();
        MaPhong.clear();
        String Where = " MaLoaiPhong = '"+MaLoaiPhong.get(cmbloaiphong.getSelectedIndex()) +"' ";
        String date = (dtngaythue.getDate().getMonth() + 1 )+"/"+dtngaythue.getDate().getDate()+ "/" + (dtngaythue.getDate().getYear() + 1900);
        Where += " and ( TinhTrang = '1' "
                +" or ( MaPhong in (Select MaPhong From PhieuThue Where NgayDi <= '"+date+"') "
                +" and MaPhong not in ( Select MaPhong From PhieuThue where NgayDi > '"+ date +"'))) ";
       // cong them phong da dat neu dang sua
        if(btnSua1.isEnabled() && (MaLoaiPhong.get(cmbloaiphong.getSelectedIndex()).toString() == null ? MaLP == null : MaLoaiPhong.get(cmbloaiphong.getSelectedIndex()).toString().equals(MaLP)))
        {
            Where += " or MaPhong = '"+ MP+"' ";
            
        }
        
        ResultSet rs = PhongController.phong_GetByTop("", Where, null);
        if(rs != null)
        {
            while(rs.next())
            {
                cmbmaphong.addItem(rs.getString("TenPhong"));
                MaPhong.add(rs.getString("MaPhong").toString());
            }
        }
        if(cmbmaphong.getItemCount()== 0)
            lbcanhbao.setVisible(true);
        else
            lbcanhbao.setVisible(false);
       }
   }
   catch(Exception e)
   {
        //JOptionPane.showMessageDialog(rootPane, "ERROR");
   }
}//GEN-LAST:event_cmbloaiphongItemStateChanged

private void cmdichvuItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmdichvuItemStateChanged

    try
   {
       if(active)
       {
           txtgiadichvu.setText(GiaDV.get(MaDV.get(cmdichvu.getSelectedIndex()).toString()).toString());
           txtsoluong.setText(SoLuong.get(cmdichvu.getSelectedIndex()).toString());
       }
   }
   catch(Exception e)
   {}
}//GEN-LAST:event_cmdichvuItemStateChanged

private void btnSua1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSua1ActionPerformed
try {
            
            //kiem tra input
            if(!checkinput()) return;

            //Luu thong tin khac hang
            KhachHangDTO kh = new KhachHangDTO();
            kh.setTenKH(txthoten.getText());
            kh.setGioiTinh(cmbgioitinh.getSelectedItem().toString());
            kh.setDiaChi(txtdiachi.getText());
            kh.setNgaySinh((dtngaysinh.getDate().getMonth() + 1 )+"/"+ dtngaysinh.getDate().getDate()+ "/" + (dtngaysinh.getDate().getYear() + 1900));
            kh.setQuocTich(lcquoctich.getSelectedItem().toString());
            kh.setCMND(txtcmnd.getText());
            kh.setSDT(txtsodienthoai.getText());
            kh.setMaKH(MaKH);

            KhachHangController.khachHang_Update(kh);
            
            //luu phieu thue
            PhieuThueDTO pt = new PhieuThueDTO();
            pt.setMaKH(MaKH);
            pt.setMaPhong((String) MaPhong.get(cmbmaphong.getSelectedIndex()));
            pt.setNgayDi((dtngaydi.getDate().getMonth() + 1 )+ "/"+ dtngaydi.getDate().getDate()+ "/" + (dtngaydi.getDate().getYear() + 1900));
            pt.setNgayThue((dtngaythue.getDate().getMonth() + 1 )+ "/"+ dtngaythue.getDate().getDate()+ "/" + (dtngaythue.getDate().getYear() + 1900));
            pt.setMaPhieuThue(MaPT);

            PhieuThueController.phieuThue_Update(pt);
            
            //cap nhat tinh trang phong
            if(!pt.getMaPhong().equals(MP))//thay doi phong khac
            {
                ResultSet rs =  PhieuThueController.phieuThue_GetByTop("", " MaPhong = '"+MP+"' ", "");
                if(rs != null)
                {
                    try {
                        rs.next();
                        if(rs.getRow() == 0)//Neu phong dat truoc do khong co ai thue thi set lai phong trong
                        {
                            PhongController.phong_UpdateStatus(MP, "1");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(frmDatPhong.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
                //cap nhat tinh trang phong moi chon
                PhongController.phong_UpdateStatus(pt.getMaPhong(), "0");
                MP = pt.getMaPhong();//luu lai gia tri cho MP temp
            }

            //luu phieu thue dich vu
                //Xoa cac dv da dat
            PhieuThueDVController.phieuThueDV_Delete(MaPT);
            PhieuThueDVDTO dv = new PhieuThueDVDTO();
            for(int i = 0; i< MaDV.size();i++)
            {
                dv.setMaPhieuThue(MaPT);
                dv.setMaPTDV(MaDV.get(i).toString());
                dv.setSoLuong(SoLuong.get(i).toString());
                dv.setGhiChu("");
                PhieuThueDVController.phieuThueDV_Insert(dv);
            }
            JOptionPane.showMessageDialog(rootPane, "Cập nhật thành công!");

            // TODO add your handling code here:
        } catch (Exception ex) {
            Logger.getLogger(frmDatPhong.class.getName()).log(Level.SEVERE, null, ex);
        }
    // TODO add your handling code here:
}//GEN-LAST:event_btnSua1ActionPerformed

private void btnXoa1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoa1ActionPerformed
    try
    {
        
        if(  JOptionPane.showConfirmDialog(null,  "Bạn có muốn hủy đặt phòng không??"
                        , "Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE) == JOptionPane.YES_OPTION)
        {
            //xoa phieu dich vu
            PhieuThueDVController.phieuThueDV_Delete(MaPT);
            //Xoa phieu dat phong
            PhieuThueController.phieuThue_Delete(MaPT);
            //Xoa khach hang
            KhachHangController.khachHang_Delete(MaKH);
            //cap nhat lai trang thai phong
            ResultSet rs =  PhieuThueController.phieuThue_GetByTop("", " MaPhong = '"+MP+"' ", "");
            if(rs != null)
            {
               try {
                       rs.next();
                        if(rs.getRow() == 0)//Neu phong dat truoc do khong co ai thue thi set lai phong trong
                        {
                            PhongController.phong_UpdateStatus(MP, "1");
                        }
                    } catch (SQLException ex) {
                        Logger.getLogger(frmDatPhong.class.getName()).log(Level.SEVERE, null, ex);
                    }
             }
           //reset du lieu
            setreset();
            
            JOptionPane.showMessageDialog(rootPane, "Hủy thành công!");
        }
    }
    catch(Exception e)
    {}
    // TODO add your handling code here:
}//GEN-LAST:event_btnXoa1ActionPerformed

private void txtcmndKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtcmndKeyTyped
    char c = evt.getKeyChar();
    if(!Character.isDigit(c) )
    {
        evt.consume();
    }
    // TODO add your handling code here:
}//GEN-LAST:event_txtcmndKeyTyped

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    if(preframe == 0)
    {
        frmPhieuThue._frm.getselectwhere();
        frmPhieuThue._frm.setVisible(true);
        frmPhieuThue._frm.setFocusable(true);
    }
    else
        if(preframe == 1)
        {
            frmMain._main.setVisible(true);
            frmMain._main.setFocusable(true);
        }
    else
        if(preframe == 2)
        {
            frmTraCuu._frmtc.setVisible(true);
            frmTraCuu._frmtc.setFocusable(true);
        }
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowClosed
private frmDatDichVu frmddv; 
private void btdatdichvuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btdatdichvuActionPerformed
    if(frmddv != null)
    {
    }
    else
        frmddv = new frmDatDichVu();
    
    frmddv.setVisible(true);
    frmddv.setFocusable(true);
    this._frm = this;
    this.setVisible(false);
    // TODO add your handling code here:
}//GEN-LAST:event_btdatdichvuActionPerformed

private void dtngaythuePropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_dtngaythuePropertyChange
    
    if(!"null".equals(dtngaydi.getDate()+"")&&!"null".equals(dtngaythue.getDate()+""))
    {
        active = false;
        cmbloaiphong.setSelectedIndex(-1);
        active = true;
        cmbloaiphong.setSelectedIndex(0);
    }
    else
    {
        cmbmaphong.setSelectedIndex(-1);
    }

}//GEN-LAST:event_dtngaythuePropertyChange
    public void setdichvu()
    {
        cmdichvu.removeAllItems();
        SoLuong.clear();
        for(int i=0;i<MaDV.size();i++)
        {
            cmdichvu.addItem(BangDV.getValueAt(i, 0).toString());
            SoLuong.add(BangDV.getValueAt(i, 1).toString());
            
        }
        
        if(cmdichvu.getSelectedIndex() != -1)
        {
            btdatdichvu.setText("Sửa dịch vụ");
            txtsoluong.setText(SoLuong.get(0).toString());
        }
        else
            btdatdichvu.setText("Đặt dịch vụ");
    }
        /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmDatPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmDatPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmDatPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmDatPhong.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmDatPhong().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btdatdichvu;
    private javax.swing.JButton btnSua1;
    private javax.swing.JButton btnThem1;
    private javax.swing.JButton btnXoa1;
    private javax.swing.JComboBox cmbgioitinh;
    private javax.swing.JComboBox cmbloaiphong;
    private javax.swing.JComboBox cmbmaphong;
    private javax.swing.JComboBox cmdichvu;
    private com.toedter.calendar.JDateChooser dtngaydi;
    private com.toedter.calendar.JDateChooser dtngaysinh;
    private com.toedter.calendar.JDateChooser dtngaythue;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JLabel lbGia;
    private javax.swing.JLabel lbcanhbao;
    private com.toedter.components.JLocaleChooser lcquoctich;
    private javax.swing.JTextField txtcmnd;
    private javax.swing.JTextField txtdiachi;
    private javax.swing.JTextField txtgiadichvu;
    private javax.swing.JTextField txtgiaphong;
    private javax.swing.JTextField txthoten;
    private javax.swing.JTextField txtmakh;
    private javax.swing.JTextField txtmapt;
    private javax.swing.JTextField txtsodienthoai;
    private javax.swing.JTextField txtsoluong;
    // End of variables declaration//GEN-END:variables
}


